//
//  DrawLiveEcg.m
//  Ikinloop-demo
//
//  Created by Li Shen on 2021/4/22.
//  Copyright © 2021 lyh. All rights reserved.
//

#import "DrawLiveEcg.h"
#import "DrawUtils.h"

@interface DrawLiveEcg()

@property (nonatomic, strong) CALayer *layer;
@property (nonatomic, assign) CGContextRef context;

@property (nonatomic) NSInteger drawPointNum;
@property (nonatomic) NSInteger drawLineNum;
@property (nonatomic) CGFloat dataStartX;
@property (nonatomic) CGFloat dataStartY;
@property (nonatomic) CGFloat lastStartX;
@property (nonatomic) CGFloat lastStartY;
@property (nonatomic) NSInteger mmPixels;
@property (nonatomic) CGFloat width;
@property (nonatomic) CGFloat height;

@property (nonatomic) int maxPoint;

@property (nonatomic) CGFloat xStepPixelPerPoint;
@property (nonatomic) CGFloat yStepPixelPerUv;


@property (nonatomic, strong) NSMutableArray *ecgStatePosArr;
@property (nonatomic, strong) DrawUtils *drawUils;

@property (nonatomic, strong) CAShapeLayer *shapelayer;
//贝塞尔曲线路径
@property (nonatomic, strong)UIBezierPath *bezierpath;

@property (nonatomic, strong) NSMutableArray *shapelayerArr;


@end
@implementation DrawLiveEcg
- (instancetype)initWithLayer:(CALayer*)layer
{
    self = [super init];
    if (self) {
        self.layer = layer;
        [self setupDefaultValues];
    }
    
    return self;
}

- (instancetype)initWithLayer:(CALayer*)layer context:(CGContextRef)context{
    self = [super init];
    if (self) {
        self.layer = layer;
        self.context = context;
        [self setupDefaultValues];
    }
    
    return self;
}

- (void)setupDefaultValues{
    self.drawUils = [[DrawUtils alloc] initWithLayer:self.layer context:self.context];
    self.mmPixels = [DrawUtils mm2pt:1.0];
    self.ecgGain = 10;
    self.ecgSpeed = 25;
    self.sampleRate = 512;
    self.lineWidth = 1;
    self.dataStartX = 0;
    self.dataStartY =  4 * 5 * self.mmPixels;
    self.ecgColor = [UIColor greenColor];
    self.lastStartX = 0;
    self.lastStartY = 0;
    self.drawLineNum = 0;
    self.drawPointNum = 0;
    
    self.width = self.layer.frame.size.width;
    self.height = self.layer.frame.size.height;

    self.xStepPixelPerPoint = (_ecgSpeed / _sampleRate) * _mmPixels;
    self.yStepPixelPerUv = (_ecgGain * _mmPixels) / 1000;
    
    self.maxPoint = (int)(self.width / _xStepPixelPerPoint);
    self.bezierpath = [[UIBezierPath alloc] init];
    self.shapelayer = [[CAShapeLayer alloc] init];
    self.shapelayerArr = [[NSMutableArray alloc] init];
}

- (void)reset{
    self.dataStartX = 0;
    self.dataStartY =  4 * 5 * self.mmPixels;
    
    self.lastStartX = 0;
    self.lastStartY = 0;
    self.drawLineNum = 0;
    self.drawPointNum = 0;
    [self.shapelayerArr makeObjectsPerformSelector:@selector(removeFromSuperlayer)];
    [self.shapelayerArr removeAllObjects];
}

- (void)drawWaveByRepeat:(int)ecgdata{
    CGFloat xStepPixelPerPoint = self.xStepPixelPerPoint;
    CGFloat yStepPixelPerUv = self.yStepPixelPerUv;
    
    UIBezierPath* bezierpath = [[UIBezierPath alloc] init];
    CAShapeLayer* shapelayer = [[CAShapeLayer alloc] init];
    _drawLineNum++;
    
    if(_drawPointNum == 0){
        _lastStartX = _dataStartX;
        _lastStartY = _dataStartY - (ecgdata *yStepPixelPerUv);
        _drawPointNum ++;
        return;
    }
    CGFloat pointX = _dataStartX + (_drawPointNum * xStepPixelPerPoint);
    if(pointX >= self.width){
        _drawPointNum = 0;
        _lastStartX = _dataStartX;
        _lastStartY = _dataStartY - (ecgdata * yStepPixelPerUv);
        _drawPointNum++;
        return;
    }
 
    [bezierpath moveToPoint:CGPointMake(_lastStartX, _lastStartY)];
   
    
    CGFloat x = _dataStartX + _drawPointNum * xStepPixelPerPoint;
    CGFloat y = _dataStartY - (ecgdata * yStepPixelPerUv);
    [self.drawUils addLineToBezierpath:bezierpath toEndPoint:CGPointMake(x, y) withLineWidth:_lineWidth];
    
    [self.drawUils addBezierpath:shapelayer withBezierpath:bezierpath withColor:self.ecgColor];
    
    [self.layer replaceSublayer:shapelayer with:shapelayer];
    int pointStep = _drawLineNum % _maxPoint;
    
    if(self.shapelayerArr.count >= _maxPoint){
        CAShapeLayer *preLayer = [self.shapelayerArr objectAtIndex:pointStep];
        [self.layer replaceSublayer:preLayer with:shapelayer];
        [self.shapelayerArr replaceObjectAtIndex:pointStep withObject:shapelayer];
    }else{
        [self.layer addSublayer:shapelayer];
        [self.shapelayerArr addObject:shapelayer];
    }

    _lastStartX = x;
    _lastStartY = y;
    
    _drawPointNum++;
}

- (void)drawWaveFullScreen:(NSArray*)ecgdatas{
    CGFloat xStepPixelPerPoint = self.xStepPixelPerPoint;
    CGFloat yStepPixelPerUv = self.yStepPixelPerUv;
    
    _drawPointNum = 0;
    CGFloat lastStartX = _dataStartX;
    CGFloat lastStartY = _dataStartY;
    
    for (int i = 0; i < ecgdatas.count; i++) {
        int value = ((NSNumber*)ecgdatas[i]).intValue;
        
        //若大于画布宽度时,不画
        CGFloat pointX = _dataStartX + (_drawPointNum * xStepPixelPerPoint);
        if(pointX >= self.width){
            break;
        }
        [self.bezierpath moveToPoint:CGPointMake(lastStartX, lastStartY)];
        
        CGFloat x = _dataStartX + _drawPointNum * xStepPixelPerPoint;
        CGFloat y = _dataStartY - (value * yStepPixelPerUv);
        [self.drawUils addLineToBezierpath:self.bezierpath toEndPoint:CGPointMake(x, y) withLineWidth:_lineWidth];
        
        
        lastStartX = x;
        lastStartY = y;
        _drawPointNum++;
    }
    [self.drawUils addShapelayer:self.shapelayer withBezierpath:self.bezierpath withColor:self.ecgColor];
}

@end
